package com.blensmile.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

import com.blensmile.BaseActivity;
import com.blensmile.R;
import com.blensmile.utils.BlenBounceInterpolator;

public class BounceActivity extends BaseActivity implements View.OnClickListener{

    private View imgRed;
    private TranslateAnimation trans;
    private AnimatorSet set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bounce);
        imgRed = findViewById(R.id.img_red);
        imgRed.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_red:
//                imgRed.clearAnimation();
//                imgRed.startAnimation(trans);
                if(set == null){
                    set = new AnimatorSet();

                    ObjectAnimator animatorX = ObjectAnimator.ofFloat(imgRed,"translationX",0,((View)imgRed.getParent()).getWidth()-imgRed.getWidth());
                    animatorX.setInterpolator(new LinearInterpolator());
                    ObjectAnimator animatorY = ObjectAnimator.ofFloat(imgRed,"translationY",0,((View)imgRed.getParent()).getHeight()-imgRed.getHeight());
                    animatorY.setInterpolator(new BounceInterpolator());
                    set.setDuration(2000);
                    set.playTogether(animatorX,animatorY);
                }
                if(set.isRunning()){
                    set.cancel();
                }else {
                    set.start();
                }
        }
    }
}
